//
//  TabTagCell.m
//  danpin
//
//  Created by chuangjia on 28/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "TabTagCell.h"

@interface TabTagCell()
{
    
}

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *btnCat2;
@property (nonatomic, strong) UIButton *btnBrand;
@property (nonatomic, strong) UIButton *btnGood;
@property (nonatomic, strong) UIButton *btnnew;

@property (nonatomic, strong) UIImageView *imgBorderView1;
@property (nonatomic, strong) UIImageView *imgBorderView2;
@property (nonatomic, strong) UIImageView *imgBorderView3;
@property (nonatomic, strong) UIImageView *imgBorderView4;
@end


@implementation TabTagCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        _bgView.backgroundColor =[UIColor clearColor];
        [self.contentView addSubview:_bgView];
        //
        _btnCat2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCat2 setTitle:@"最新" forState:UIControlStateNormal];
        _btnCat2.backgroundColor = UIColorFromRGB(0xd21d1d);
        [_btnCat2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btnCat2.tag = 0;
        [_btnCat2 addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnCat2.titleLabel.font = FONT(14);
        _btnCat2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        _btnCat2.layer.cornerRadius = 3;//(值越大，角就越圆)
        _btnCat2.layer.masksToBounds = NO;
        _btnCat2.layer.borderWidth =0.5;
        _btnCat2.layer.borderColor =UIColorFromRGB(0xe1e1e1).CGColor;
        _btnCat2.layer.rasterizationScale = [UIScreen mainScreen].scale;
        _btnCat2.layer.shouldRasterize = YES;

        _btnBrand = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnBrand setTitle:@"晒单" forState:UIControlStateNormal];
        _btnBrand.backgroundColor = [UIColor whiteColor];
        _btnBrand.tag = 1;
        [_btnBrand setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btnBrand addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnBrand.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        _btnBrand.titleLabel.font = FONT(14);
        _btnBrand.layer.cornerRadius = 3;//(值越大，角就越圆)
        _btnBrand.layer.masksToBounds = NO;
        _btnBrand.layer.borderWidth =0.5;
        _btnBrand.layer.borderColor =UIColorFromRGB(0xe1e1e1).CGColor;

        _btnBrand.layer.rasterizationScale = [UIScreen mainScreen].scale;
        _btnBrand.layer.shouldRasterize = YES;
        
        
        _btnGood = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnGood setTitle:@"推荐" forState:UIControlStateNormal];
        _btnGood.backgroundColor =[UIColor whiteColor];
        [_btnGood setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _btnGood.titleLabel.font = FONT(14);
        _btnGood.tag = 2;
        [_btnGood addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside];
        
        _btnGood.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        _btnGood.layer.cornerRadius = 3;//(值越大，角就越圆)
        _btnGood.layer.masksToBounds = NO;
        _btnGood.layer.borderColor =UIColorFromRGB(0xe1e1e1).CGColor;
        _btnGood.layer.borderWidth =0.5;
        _btnGood.layer.rasterizationScale = [UIScreen mainScreen].scale;
        _btnGood.layer.shouldRasterize = YES;
        
        _btnnew = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnnew setTitle:@"榜单" forState:UIControlStateNormal];
        _btnnew.backgroundColor =[UIColor whiteColor];
        [_btnnew setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _btnnew.titleLabel.font = FONT(14);
        _btnnew.tag =3;
        [_btnnew addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnnew.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        _btnnew.layer.cornerRadius = 3;//(值越大，角就越圆)
        _btnnew.layer.masksToBounds = NO;
        _btnnew.layer.borderColor =UIColorFromRGB(0xe1e1e1).CGColor;
        _btnnew.layer.borderWidth =0.5;
        _btnnew.layer.rasterizationScale = [UIScreen mainScreen].scale;
        _btnnew.layer.shouldRasterize = YES;
      
        [_bgView addSubview:_btnnew];
        
        _imgBorderView4 =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imgBorderView4.image =[UIImage imageNamed:@"border_04.png"];
        [_bgView addSubview:_imgBorderView4];
        
        [_bgView addSubview:_btnGood];
        //
        _imgBorderView3 =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imgBorderView3.image =[UIImage imageNamed:@"border_03.png"];
        [_bgView addSubview:_imgBorderView3];
        
        [_bgView addSubview:_btnBrand];
        
        _imgBorderView2 =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imgBorderView2.image =[UIImage imageNamed:@"border_02.png"];
        [_bgView addSubview:_imgBorderView2];
        
        [_bgView addSubview:_btnCat2];
        _imgBorderView1 =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imgBorderView1.image =[UIImage imageNamed:@"border_01.png"];
        [_bgView addSubview:_imgBorderView1];
        
      
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.data=nil;
    self.other=nil;
    self.key=nil;
    self.indexPath=nil;
    
    [self.btnCat2 removeFromSuperview];
    self.btnCat2=nil;
    [self.btnBrand removeFromSuperview];
    self.btnBrand=nil;
    [self.btnGood removeFromSuperview];
    self.btnGood=nil;
    [self.btnnew removeFromSuperview];
    self.btnnew=nil;
    
    [self.imgBorderView1 removeFromSuperview];
    self.imgBorderView1=nil;
    [self.imgBorderView2 removeFromSuperview];
    self.imgBorderView2=nil;
    [self.imgBorderView3 removeFromSuperview];
    self.imgBorderView3=nil;
    [self.imgBorderView4 removeFromSuperview];
    self.imgBorderView4=nil;
    
    [self.bgView removeFromSuperview];
    self.bgView=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _bgView.frame =CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
}
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.other =other;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key = key;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
    self.data =model;
    float fTop = 0;
    fTop += PHOTO_FRAME_WIDTH*2;
    float fBtnTop = PHOTO_FRAME_WIDTH*2;
    float fLeftOfBtn = PHOTO_FRAME_WIDTH*2;
    
    //
    NSString* strCat2 = @"最新";
    CGSize size=[strCat2 sizeWithAttributes:@{NSFontAttributeName: FONT(14)}];
    float fCatWidth = ceil(size.width )+ PHOTO_FRAME_WIDTH*3 ;
    
    _btnCat2.frame =  CGRectMake(fLeftOfBtn, fBtnTop, fCatWidth, CAT_BUTTON_HEIGHT);
    
    fLeftOfBtn += fCatWidth - PHOTO_TIME_PADDING;
    
    if( CGAffineTransformIsIdentity(_btnCat2.transform))
    {
        float fRotation = ((float)(arc4random()%150)) /10000;
        if(((int)(fRotation * 10000)) % 2)
        {
            fRotation = -fRotation;
        }
        
        _btnCat2.transform = CGAffineTransformMakeRotation(M_PI * fRotation);
    }
    //
    NSString* strBrand =  @"晒单";
    CGSize size1=[strBrand sizeWithAttributes:@{NSFontAttributeName: FONT(14)}];
    float fBrandWidth = ceil(size1.width )+ PHOTO_FRAME_WIDTH*3 ;
    _btnBrand.frame =  CGRectMake(fLeftOfBtn,
                                  fBtnTop,
                                  fBrandWidth,
                                  CAT_BUTTON_HEIGHT);
    
    fLeftOfBtn += fBrandWidth - PHOTO_TIME_PADDING;
    if( CGAffineTransformIsIdentity(_btnBrand.transform))
    {
        
        float fRotation = ((float)(arc4random()%150)) /10000;
        if(((int)(fRotation * 10000)) % 2)
        {
            fRotation = -fRotation;
        }
        _btnBrand.transform = CGAffineTransformMakeRotation(M_PI * fRotation);
    }
    //
    NSString* strGood = @"推荐";
    CGSize size2=[strGood sizeWithAttributes:@{NSFontAttributeName:FONT(14)}];
    float fGoodWidth = ceil(size2.width )+ PHOTO_FRAME_WIDTH*3 + 1;
    if( (fGoodWidth + fLeftOfBtn) > (DEVICE_WIDTH - PHOTO_TIME_PADDING) )
    {
        fGoodWidth = DEVICE_WIDTH - fLeftOfBtn - PHOTO_TIME_PADDING;
    }
    _btnGood.frame =  CGRectMake(fLeftOfBtn, fBtnTop- PHOTO_TIME_PADDING/2, fGoodWidth, CAT_BUTTON_HEIGHT);
     fLeftOfBtn += fGoodWidth - PHOTO_TIME_PADDING;
    if( CGAffineTransformIsIdentity(_btnGood.transform))
    {
        float fRotation = ((float)(arc4random()%50)) /10000;
        if(((int)(fRotation * 10000)) % 2)
        {
            fRotation = -fRotation;
        }
        if( fRotation > 0)
        {
            CGRect rc = _btnGood.frame;
            rc.origin.y += (int)fRotation%(int)PHOTO_TIME_PADDING;
            _btnGood.frame = rc;
        }
        _btnGood.transform = CGAffineTransformMakeRotation(M_PI * fRotation);
    }
    //
    
    NSString* strNew = @"榜单";
    CGSize size4=[strNew sizeWithAttributes:@{NSFontAttributeName:FONT(14)}];
    float fGoodWidth1 = ceil(size4.width )+ PHOTO_FRAME_WIDTH*3 + 1;
    
    if( (fGoodWidth1 + fLeftOfBtn) > (DEVICE_WIDTH - PHOTO_TIME_PADDING) )
    {
        fGoodWidth1 = DEVICE_WIDTH - fLeftOfBtn - PHOTO_TIME_PADDING;
    }
    _btnnew.frame =  CGRectMake(fLeftOfBtn, fBtnTop- PHOTO_TIME_PADDING/2, fGoodWidth1, CAT_BUTTON_HEIGHT);
    
    if( CGAffineTransformIsIdentity(_btnnew.transform))
    {
        
        float fRotation = ((float)(arc4random()%50)) /10000;
        if(((int)(fRotation * 10000)) % 2)
        {
            fRotation = -fRotation;
        }
        
        if( fRotation > 0)
        {
            CGRect rc = _btnnew.frame;
            rc.origin.y += (int)fRotation%(int)PHOTO_TIME_PADDING;
            _btnnew.frame = rc;
        }
        _btnnew.transform = CGAffineTransformMakeRotation(M_PI * fRotation);
    }
    //
    fTop += TIME_HEIGHT;
    float fBackViewTop = 0;
    fTop += PHOTO_FRAME_WIDTH;
    fTop += PHOTO_TIME_PADDING;
    fBackViewTop = fTop + PHOTO_TIME_PADDING;
    
    _imgBorderView4.frame = CGRectMake(0, fBackViewTop - BOOK_FRAME+1, DEVICE_WIDTH, BOOK_FRAME);
    _imgBorderView3.frame = CGRectMake(0, fBackViewTop - BOOK_FRAME, DEVICE_WIDTH, BOOK_FRAME);
    _imgBorderView2.frame = CGRectMake(0, fBackViewTop - BOOK_FRAME, DEVICE_WIDTH, BOOK_FRAME);
    _imgBorderView1.frame = CGRectMake(0, fBackViewTop - BOOK_FRAME, DEVICE_WIDTH, BOOK_FRAME);
    

    [self layoutSubviews];
}


/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return PHOTO_FRAME_WIDTH*6;
  
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)tagAction:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    BEventType type =BEventType_Unknow;
    switch (btn.tag) {
        case 0:
        {
            [_btnCat2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_btnCat2 setBackgroundColor:UIColorFromRGB(0xd21d1d)];
            
            [_btnBrand setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnBrand setBackgroundColor:[UIColor whiteColor]];
            
            [_btnGood setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnGood setBackgroundColor:[UIColor whiteColor]];
            
            [_btnnew setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnnew setBackgroundColor:[UIColor whiteColor]];
            
            type =BEventType_IndexRank;
            self.key=@"latest";
        }
            break;
        case 1:
        {
            [_btnCat2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnCat2 setBackgroundColor:[UIColor whiteColor]];
            
            [_btnBrand setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_btnBrand setBackgroundColor:UIColorFromRGB(0xd21d1d)];
            
            [_btnGood setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnGood setBackgroundColor:[UIColor whiteColor]];
            
            [_btnnew setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnnew setBackgroundColor:[UIColor whiteColor]];
            type =BEventType_IndexShaiDan;
            self.key=@"shaidan";
        }
            break;
        case 2:
        {
            [_btnCat2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnCat2 setBackgroundColor:[UIColor whiteColor]];
            
            [_btnBrand setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnBrand setBackgroundColor:[UIColor whiteColor]];
            
            [_btnGood setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_btnGood setBackgroundColor:UIColorFromRGB(0xd21d1d)];
            
            [_btnnew setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnnew setBackgroundColor:[UIColor whiteColor]];
            type =BEventType_IndexRecommed;
            self.key=@"recommend";
        }
            break;
        case 3:
        {
            [_btnCat2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnCat2 setBackgroundColor:[UIColor whiteColor]];
            
            [_btnBrand setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnBrand setBackgroundColor:[UIColor whiteColor]];
            
            [_btnGood setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_btnGood setBackgroundColor:[UIColor whiteColor]];
            
            [_btnnew setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_btnnew setBackgroundColor:UIColorFromRGB(0xd21d1d)];
            type =BEventType_IndexNew;
            self.key=@"ranking";
        }
            break;
        default:
            break;
    }
    if (self.actionBlock) {
        self.actionBlock(type,sender,self.data,self.key,self.indexPath);
    }
}


@end
